package com.example.config;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.HeadersExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * Headers:
 * HeadersExchange 会根据消息的 Header 将消息路由到不同的 Queue 上，这种策略也和 routingkey无关
 * */
@Configuration
public class HeaderConfig {

    public static final String HEADER_QUEUE_NAME = "header_queue_name";
    public static final String HEADER_QUEUE_NAME2 = "header_queue_name2";
    public static final String HEADER_EXCHANGE_NAME = "header_exchange_name";

    @Bean
    Queue headerQueue() {
        return new Queue(HEADER_QUEUE_NAME, true, false, false);
    }

    @Bean
    Queue headerQueue2() {
        return new Queue(HEADER_QUEUE_NAME2, true, false, false);
    }

    /**
     * 定义一个直连交换机
     *
     * @return
     */
    @Bean
    HeadersExchange headerExchange() {
        //1. 交换机的名称
        //2。交换机是否持久化
        //3. 如果没有与之绑定的队列，是否删除交换机
        return new HeadersExchange(HEADER_EXCHANGE_NAME, true, false);
    }

    /**
     * 这个定义是将交换机和队列绑定起来
     *
     * @return
     */
    @Bean
    Binding headersBinding() {
        return BindingBuilder
            //设置绑定的队列
            .bind(headerQueue()).to(headerExchange()).whereAny("name","age").exist();
    }

    @Bean
    Binding headersBinding2() {
        return BindingBuilder
            //设置绑定的队列
            .bind(headerQueue2()).to(headerExchange()).where("age").matches(18);
    }
}
